package com.GPS_based_training.progetto_so2_2014_2015;

import com.GPS_based_training.progetto_so2_2014_2015.R;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class StartActivity extends ActionBarActivity {

	/******* CONSTANTS *******/
	private final String VALUE_SPEED = "VALUE_SPEED";
	private final String PREFERENCES_START_ACTIVITY = "Preferences_start_activity";
	
	/******* GRAPHIC OBJECT *******/
	private Button btnGoToTrainingSession;
	private EditText etSetSpeed;
	private Intent intentTrainingSession;
	private Intent intentHistoricalWorkouts;
	
	
	/******* LIFE-CYCLE METHODS *******/
	@Override
	/**
	 * @author Danilo Dessi'
	 * 
	 * This method creates activity
	 * @param savedInstanceState the state
	 */
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_start);
		
		//initialization graphic objects
		btnGoToTrainingSession = (Button) findViewById(R.id.btn_go_to_training_session);
		etSetSpeed = (EditText) findViewById(R.id.et_set_speed);
		
		//initializzation intents
		intentTrainingSession = new Intent(this,MainActivity.class);
		intentHistoricalWorkouts = new Intent(this, TrainingsHistoryActivity.class);
		
		//object to retrieve and save the speed that is inserted by user
		final SharedPreferences prefs = getSharedPreferences(PREFERENCES_START_ACTIVITY, Context.MODE_PRIVATE);
		
		//we update view with the previous speed that was saved
		String valueSpeedPreferences = prefs.getString(VALUE_SPEED, "10");
		etSetSpeed.setText(valueSpeedPreferences);
		
		//event of button
		btnGoToTrainingSession.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String valueSpeedText = null;
				Double valueSpeed = null;
				
				//we verify the correct insertion of the speed
				if( (valueSpeedText = etSetSpeed.getText().toString()).compareTo("") != 0) {
					
					valueSpeed = Double.parseDouble(valueSpeedText);
					
					if( valueSpeed > 0) {
						
						//we save new value of the speed in the preferences
						SharedPreferences.Editor editor = prefs.edit();
						editor.putString(VALUE_SPEED, valueSpeedText);
						editor.commit();
						
						//we pass the value of speed and start the activity for starting the training
						intentTrainingSession.putExtra(VALUE_SPEED, valueSpeed);
						startActivity(intentTrainingSession);
						
					} else {
						Toast.makeText(v.getContext(), getString(R.string.the_set_speed_is_not_correct), Toast.LENGTH_LONG).show();
					}
					
				} else {
					Toast.makeText(v.getContext(), getString(R.string.speed_is_not_set), Toast.LENGTH_LONG).show();
				}
				
			}
		});	
		
	}
	
	
	/******* CONTENT MENU METHODS *******/
	@Override
	/**
	 * @author Danilo Dessi'
	 * This method creates the options menu
	 * @param menu the menu
	 */
	public boolean onCreateOptionsMenu(Menu menu) {  
	 	 super.onCreateOptionsMenu(menu);  
	 	 getMenuInflater().inflate(R.menu.menu_start_activity, menu); 
	     
	     return true;
	     
	}

	@Override
	/**
	 * @author Danilo Dessi'
	 * 
	 * This method associates the menu items with the actions 
	 * @param item the item of the menu
	 */
	public boolean onOptionsItemSelected(MenuItem item) {
	
		switch(item.getItemId()) {
		
	        case R.id.historical_workouts:
	        	startActivity(intentHistoricalWorkouts);
	        	break;
	        	
	    	default:
	    		Toast.makeText(this, getString(R.string.operation_failed), Toast.LENGTH_LONG).show();
	        	
	     }
	       	
	         
	     return true;          
      	
	}
	
	
	
}
